//
//  DestMoreCountryViewController.m
//  TravelEveryDay
//
//  Created by dlios on 15/10/16.
//  Copyright © 2015年 com.lanou3g. All rights reserved.
//

#import "DestMoreCountryViewController.h"
#import "BaseHeader.h"
#import "DestTabBarFourImageCollectionViewCell.h"
#import "DestTabBarModel.h"
#import "DestOtherCellInfoViewController.h"
@interface DestMoreCountryViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property(strong,nonatomic)NSMutableArray *destModelArray;
@end

@implementation DestMoreCountryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    //数组初始化
    _destModelArray = [[NSMutableArray alloc]init];
    //创建collectionView
    [self createCollectionView];
    //数据请求
    [self getDestMoreCountryData];
    
}
#pragma mark - 创建 collectionView的方法
- (void)createCollectionView {
    _destFlowLayout = [[UICollectionViewFlowLayout alloc]init];
    _destFlowLayout.itemSize = CGSizeMake((SCREEN_WIDTH -25 *WIDTH)/2,(SCREEN_WIDTH -25 *WIDTH)/2 );
    _destFlowLayout.minimumInteritemSpacing = 5 *WIDTH;
    _destFlowLayout.minimumLineSpacing = 5 *HEIGHT;
    _destFlowLayout.sectionInset = UIEdgeInsetsMake(10 * HEIGHT, 10 * WIDTH, 10 * HEIGHT, 10 *WIDTH);
    _destTabBarCollectionView  = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT ) collectionViewLayout:_destFlowLayout];
    _destTabBarCollectionView.backgroundColor = [UIColor whiteColor];
    _destTabBarCollectionView.delegate = self;
    _destTabBarCollectionView.dataSource =self;
    _destTabBarCollectionView.showsHorizontalScrollIndicator = NO;
    [_destTabBarCollectionView registerClass:[DestTabBarFourImageCollectionViewCell class] forCellWithReuseIdentifier:@"destTabBarFourImageCollectionViewCell"];
    [self.view addSubview:_destTabBarCollectionView];
}
#pragma mark - collectionView的代理方法 : 返回row个数
- (NSInteger )collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _destModelArray.count;
}
#pragma mark - collectionView的代理方法 :返回cell类型
- (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
     DestTabBarFourImageCollectionViewCell  *destTabBarcell = [collectionView dequeueReusableCellWithReuseIdentifier:@"destTabBarFourImageCollectionViewCell" forIndexPath:indexPath];
    DestTabBarModel *destMoreCountryModel = [_destModelArray objectAtIndex:indexPath.row];
    //传值属性;
    destTabBarcell.destMoreCountryModel = destMoreCountryModel;
    return destTabBarcell;
}
#pragma mark - 数据请求的方法
- (void)getDestMoreCountryData {
    [DestAFNDataBlock string:[NSString stringWithFormat:@"http://api.breadtrip.com/destination/index_places/%@/",_index] AFNData:^(id block) {
        NSMutableDictionary *jsonDic = block;
        NSMutableArray *dataArray = [jsonDic objectForKey:@"data"];
        //数据处理
        for (NSMutableDictionary *dataDic in dataArray) {
            DestTabBarModel *tabBarModel =[DestTabBarModel modelWithDictionary:dataDic];
            [_destModelArray addObject:tabBarModel];
        }
        UIColor * color = [UIColor whiteColor];
        //这里我们设置的是颜色，还可以设置shadow等，具体可以参见api
        NSDictionary * dict = [NSDictionary dictionaryWithObject:color forKey: NSForegroundColorAttributeName];
        //大功告成
        self.navigationController.navigationBar.titleTextAttributes = dict;
        ////navgation的title的字符串赋值
        self.title = [jsonDic objectForKey:@"title"];
        [_destTabBarCollectionView reloadData];
    } AFNData1:^(id block) {
        
    }];
}
#pragma mark - collectionView点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    DestOtherCellInfoViewController *otherCell = [[DestOtherCellInfoViewController alloc]init];
    DestTabBarModel *destMoreCountryModel = [_destModelArray objectAtIndex:indexPath.row];
    otherCell.type = destMoreCountryModel.type;
    otherCell.MyId = destMoreCountryModel.MyId;
    [self.navigationController pushViewController:otherCell animated:YES];
}
-(void)viewWillAppear:(BOOL)animated {
    self.navigationController.navigationBarHidden = NO;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
